package www.com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.MissingServletRequestParameterException;
import www.com.entity.Film;
import www.com.entity.User;
import www.com.mapper.LoginMapper;
import www.com.service.LoginService;
import www.com.utils.JwtUtil;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginMapper loginMapper;
    @Override
    public Object pwdLogin(User body) {
        String userName = body.getUserName();
        String passWord = body.getPassWord();
       if(userName.isEmpty() || passWord.isEmpty()){
           throw new IllegalArgumentException("请输入账户名称和密码");
       }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName", userName);
        User userInfo = loginMapper.selectOne(queryWrapper);
        if(userInfo == null){
            throw new IllegalArgumentException("没有找到该用户信息，请先注册");
        }else {
            String token = JwtUtil.createJwt(userName);
            Map<String, Object> result = new HashMap<>();

            result.put("token", token);
            Map<String, String> resultUser = new HashMap<>();

            resultUser.put("avatar", userInfo.getAvatar());
            resultUser.put("role", "管理员");
            resultUser.put("userName", userName);
            result.put("userInfo", resultUser);
            return result;
        }

    }
}
